package com.jade.editor.anisotropy.editors;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.swt.graphics.Point;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.jade.editor.anisotropy.node.Cortege;
import com.jade.editor.anisotropy.node.Node;

public class AnisotropyModel {
	
	private Point _size = null;
	private ArrayList<Cortege> _clist = new ArrayList<Cortege>();
	private ArrayList<Node> _nlist = new ArrayList<Node>();
	
	private String _path = null;
	
	public AnisotropyModel(String path){
		_path = path;
		load();
	}
	
	
	public void setCortegeList(ArrayList<Cortege> list){
		_clist = list;
	}
	
	public void setNodeList(ArrayList<Node> list){
		_nlist = list;
	}
	
	public void setSize(Point size){
		_size = size;
	}
	
	public ArrayList<Cortege> getCortegeList(){
		return _clist;
	}
	
	public ArrayList<Node> getNodeList(){
		return _nlist;
	}
	
	public Point getSize(){
		return _size;
	}
	
	public void terminateData(){
		_size = null;
		_clist.clear();
		_nlist.clear();
	}
	
	
	public void load(){
		File file = new File(_path);
		
		if(file.exists()){
			if(file.length() > 0){
				

				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder;
				
				try{
					
					dBuilder = dbFactory.newDocumentBuilder();
					Document doc = dBuilder.parse(file);
					doc.getDocumentElement().normalize();
					
					if(doc.hasChildNodes()){
						
						if(doc.getDocumentElement().getNodeName().toLowerCase().indexOf("anisotropy") == 0){
							
							//--SIZE
							if(doc.getDocumentElement().hasAttributes()){
								
								String xString = doc.getDocumentElement().getAttributeNode("x").getValue();
								String yString = doc.getDocumentElement().getAttributeNode("y").getValue();
								
								int x = Integer.parseInt(xString);
								int y = Integer.parseInt(yString);
								
								_size = new Point(x, y);
								
							}
							
							NodeList rootNodeList = doc.getDocumentElement().getChildNodes();
							
							for(int i = 0; i < rootNodeList.getLength(); i++){
								
								//---Cortege
								if(rootNodeList.item(i).getNodeName().toLowerCase().equals("cortege")){
									if(rootNodeList.item(i).hasChildNodes()){
										NodeList cortegeNodeList = rootNodeList.item(i).getChildNodes();
						
										String xString = ((Element) cortegeNodeList.item(1)).getAttribute("x");
										String yString = ((Element) cortegeNodeList.item(1)).getAttribute("y");
										
										int x = Integer.parseInt(xString);
										int y = Integer.parseInt(yString);
										
										Node n1 = new Node(x,y);
										
										xString = ((Element) cortegeNodeList.item(cortegeNodeList.getLength()-2)).getAttribute("x");
										yString = ((Element) cortegeNodeList.item(cortegeNodeList.getLength()-2)).getAttribute("y");
										
										x = Integer.parseInt(xString);
										y = Integer.parseInt(yString);
										
										Node n2 = new Node(x,y);
										
										_clist.add(new Cortege(n1, n2));
									}
								}
								
								//---Point
								if(rootNodeList.item(i).getNodeName().toLowerCase().equals("point")){
									String xString = ((Element) rootNodeList.item(i)).getAttribute("x");
									String yString = ((Element) rootNodeList.item(i)).getAttribute("y");
									
									int x = Integer.parseInt(xString);
									int y = Integer.parseInt(yString);
									
									_nlist.add(new Node(x,y));
								}
								
							}
							
						}
						
					}
					
				}
				catch(Exception e){
					e.printStackTrace();
				}
				
				
			}
		}
	}
	
	
	
	
	
	
	
	public void save(){
		File file = new File(_path);
		
		try{
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			
			//--Ani
			
			Element rootElement = doc.createElement("Anisotropy");
			rootElement.setAttribute("x", Integer.toString(_size.x));
			rootElement.setAttribute("y", Integer.toString(_size.y));
			doc.appendChild(rootElement);
			
			//--Corteges
			
			for(Cortege c : _clist){
				Element cortegeElement = doc.createElement("CORTEGE");
				ArrayList<Point> plist = c.getPointsList();
				
				for(Point p : plist){
					Element pointElement = doc.createElement("POINT");
					
					pointElement.setAttribute("x", Integer.toString(p.x));
					pointElement.setAttribute("y", Integer.toString(p.y));
					
					cortegeElement.appendChild(pointElement);
				}
				
				rootElement.appendChild(cortegeElement);
			}
			
			for(Node n : _nlist){
				Element nodeElement = doc.createElement("POINT");
				nodeElement.setAttribute("x", Integer.toString(n.x));
				nodeElement.setAttribute("y", Integer.toString(n.y));
				rootElement.appendChild(nodeElement);
			}
			
			//---WRITE TO FILE
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(file);
	 
			transformer.transform(source, result);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		
	}

}
